import java.util.Arrays;

public class MyArraylist {

    public int[] elem;
    public int usedSize;//0
    //默认容量
    private static final int DEFAULT_SIZE = 5;

    public MyArraylist() {
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 打印顺序表:
     *   根据usedSize判断即可
     */
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print (elem[i]+" ");
        }
        System.out.println();
    }


    /**
     * 判断当前的顺序表是不是满的！
     * @return true:满   false代表空
     */
    public boolean isFull() {

        if(usedSize < elem.length){
            return false;
        }
        return true;
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if(isFull()){
            //满了，扩容 2倍
            this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
        }
        elem[this.usedSize] = data;
        usedSize++;
    }


    // 在 pos 位置新增元素
    public void add(int pos, int data) throws PosWrongfulException {
        //检查满没有
       if( isFull()){
           System.out.println("数组满了");
           this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
       }
       //pos位置合法型
        if(pos<0 || pos>this.usedSize)
        {
            System.out.println("pos位置不合法！");
            throw new PosWrongfulException("pos位置不合法");
        }

            for (int i = this.usedSize; i > pos ; i--) {
                this.elem[i] = this.elem[i-1];
            }
            this.elem[pos] = data;
            this.usedSize++;
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) throws NullPointerException {
        //空指针
        if( this.elem == null ){
            System.out.println("当前顺序表指针为空！");
            throw new NullPointerException("空指针！");
        }
        //遍历数组找
        for (int i = 0; i < this.elem.length; i++) {
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) throws NullPointerException{
            for (int i = 0; i < this.elem.length; i++) {
                if(this.elem[i] == toFind){
                    return i;
                }
            }
        return -1;
    }

    // 获取 pos 位置的元素
    public int get(int pos) throws PosWrongfulException{
        //pos位置合法型
        if(pos<0 || pos>=this.usedSize)
        {
            System.out.println("pos位置不合法！");
            throw new PosWrongfulException("pos位置不合法");
        }
            return this.elem[pos];
    }

    private boolean isEmpty() {
        return this.usedSize == 0;
    }
    // 给 pos 位置的元素设为【更新为】 value （替换不能插入）
    public void set(int pos, int value) throws NullPointerException,PosWrongfulException{
        //空指针
        if( this.elem == null ){
            System.out.println("当前顺序表指针为空！");
            throw new NullPointerException("空指针！");
        }
        //pos位置合法型
        if(pos<0 || pos>this.usedSize)
        {
            System.out.println("pos位置不合法！");
            throw new PosWrongfulException("pos位置不合法");
        }
        this.elem[pos] = value;
    }

    /**
     * 删除第一次出现的关键字key
     * @param key
     */
    public void remove(int key) throws NullPointerException{
        if(this.contains(key)){
            int index = indexOf(key);
            for (int j = index; j < this.usedSize-1; j++) {
                this.elem[j] = this.elem[j+1];
            }
            this.usedSize--;
        }
    }

    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
        this.elem = null;
    }
}